package com.hjj.controller;

import com.hjj.beans.DictionaryType;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.CRUDException;
import com.hjj.services.DictionaryTypeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.UUID;

@RestController
@RequestMapping("/DictionaryType")
public class DictionaryTypeController {
    @Autowired
    @Qualifier("dictionaryTypeServicesImp")
    DictionaryTypeServices dictionaryTypeServices;

    @RequestMapping("getAll.action")
    public ArrayList<DictionaryType> getAll() {
        return dictionaryTypeServices.getAll();
    }

    @RequestMapping(value = "add.action", method = RequestMethod.POST)
    public ResultDTO add(DictionaryType dictionaryType) throws CRUDException {
        // 获取一个UUID
        UUID uuid = UUID.randomUUID();

        // 将UUID添加到DIctionaryType中
        dictionaryType.setCode(uuid.toString().replace("-", ""));

        boolean add = dictionaryTypeServices.add(dictionaryType);

        if (!add) {
            throw new CRUDException("字典类型数据添加失败");
        }


        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }

    @RequestMapping("editBefore.action")
    public DictionaryType editBefore(String id) {
        return dictionaryTypeServices.get(id);
    }

    @RequestMapping(value = "edit.action", method = RequestMethod.POST)
    public ResultDTO edit(DictionaryType dictionaryType) throws CRUDException {
        boolean edit = dictionaryTypeServices.edit(dictionaryType);

        if (!edit) {
            throw new CRUDException("字典类型数据修改失败");
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }

    @RequestMapping(value = "del.action", method = RequestMethod.POST)
    public ResultDTO del(@RequestBody String[] ids) throws CRUDException {
        boolean del = dictionaryTypeServices.del(ids);

        if (!del) {
            throw new CRUDException("字典类型数据删除失败");
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }


}
